class Solution
{
public:
    int uniqueLetterString(string s)
    {
        vector<int> charIndexes[26];
        int n = s.size();
        for (int c = 0; c < 26; ++c)
        {
            charIndexes[c].push_back(-1);
        }
        for (int i = 0; i < n; ++i)
        {
            charIndexes[s[i] - 'A'].push_back(i);
        }
        for (int c = 0; c < 26; ++c)
        {
            charIndexes[c].push_back(n);
        }
        int result = 0;
        for (int c = 0; c < 26; ++c)
        {
            auto &charIndex = charIndexes[c];
            for (int i = 1; i < charIndex.size() - 1; ++i)
            {
                result += (charIndex[i + 1] - charIndex[i]) * (charIndex[i] - charIndex[i - 1]);
            }
        }
        return result;
    }
};